<template>
  <div class="app-container">
    <div class="page-card">
      <div class="page-form">
        <el-input
          v-model="queryParams.subjectName"
          placeholder="科目名称"
          clearable
          @input="handleSearch"
        />
        <el-select
          v-model="queryParams.generalSubjectType"
          placeholder="是否综合科目"
          clearable
          @change="handleSearch"
        >
          <el-option
            v-for="dict in dict.type.is_composite_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </div>
      <el-table
        stripe
        border
        ref="tableList"
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        row-key="subjectId"
      >
        <!-- <el-table-column
          type="selection"
          width="55"
          align="center"
          :reserve-selection="true"
        /> -->
        <el-table-column label="科目名称" align="center">
          <template slot-scope="scope">
            <km-tag :option="scope.row.subjectName"></km-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="科目类型"
          align="center"
          prop="generalSubjectName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="包含学科"
          align="center"
          prop="subSubjectName"
          :show-overflow-tooltip="true"
          ><template slot-scope="scope">
            <km-tag :option="scope.row.subSubjectName" separator=" "></km-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-link type="primary" @click="edit(scope.row)">编辑</el-link>
            <el-link type="danger" @click="del(scope.row.subjectId)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="page-bottom">
      <div class="sel-box">
        <p>共 {{ total }} 项</p>
        <el-button type="primary" @click="edit(false)" v-hasRole="['admin']"
          >新增科目</el-button
        >
        <!-- <el-button type="danger" :disabled="multiple" @click="del(false)"
          >批量删除</el-button
        > -->
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-dialog
      ref="editDialog"
      class="editKmVisible"
      title="创建/编辑新科目"
      :visible.sync="editKmVisible"
      width="436px"
      :before-close="beforeClose"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="rules"
        label-position="left"
        label-width="140px"
      >
        <el-form-item label="添加学科名称" prop="subjectName">
          <el-input
            v-model="editForm.subjectName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="综合科目">
          <el-switch
            v-model="editForm.generalSubjectType"
            active-color="#1890ff"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="选择考试科目" prop="subSubjectId">
          <el-select
            v-model="editForm.subSubjectId"
            multiple
            :disabled="editForm.generalSubjectType == '0'"
          >
            <el-option
              v-for="dict in xklb"
              :key="dict.subjectId"
              :label="dict.subjectName"
              :value="dict.subjectId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">返 回</el-button>
        <el-button type="primary" @click="editSubmit" :loading="loading"
          >保 存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  subjectlist,
  basicsubject,
  basicsubjectedit,
  basicsubjecteditdelete,
} from "@/api/jcgl/kmgl";
import { debounce } from "lodash";
import { subjectlistselect } from "@/api/common";
export default {
  name: "Basic_kmgl",
  dicts: ["is_composite_type", "disciplines_type"],
  data() {
    return {
      firstLoad: true,
      loading: false,

      ids: [],

      total: 0,
      tableData: [],

      multiple: true,

      queryParams: {
        pageNum: 1,
        pageSize: 15,
        subjectName: "",
        generalSubjectType: "",
      },

      editKmVisible: false,
      editForm: {
        subjectName: "",
        generalSubjectType: 0,
        subSubjectId: [],
      },
      rules: {
        subjectName: [
          { required: true, message: "科目名称不能为空", trigger: "blur" },
        ],
      },

      xklbAll: [],
      xklb: [],
    };
  },
  created() {
    this.getList();
    subjectlistselect("-1").then((res) => {
      this.xklbAll = res.data;
    });
  },
  activated() {
    if (!this.firstLoad) {
      this.getList();
    }
    this.firstLoad = false;
  },
  methods: {
    getList() {
      this.loading = true;
      this.tableData = [];
      subjectlist(this.queryParams)
        .then((response) => {
          this.tableData = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.subjectId);
      this.multiple = !selection.length;
    },

    edit(row) {
      if (row) {
        this.xklb = this.xklbAll.filter((item) => {
          return item.subjectId != row.subjectId;
        });
        this.editForm.subjectId = row.subjectId;
        this.editForm.subjectName = row.subjectName;
        this.editForm.generalSubjectType = row.generalSubjectType;
        if (row.subSubjectId) {
          this.editForm.subSubjectId = row.subSubjectId
            .split(",")
            .map((item) => {
              return Number(item);
            });
        }
      } else {
        this.xklb = this.xklbAll;
      }

      this.editKmVisible = true;
    },

    beforeClose(done) {
      this.editForm = {
        subjectName: "",
        generalSubjectType: 0,
        subSubjectId: [],
      };
      this.$nextTick(() => {
        this.$refs.editForm.clearValidate();
        done();
      });
    },
    handleClose() {
      this.beforeClose(() => {
        this.editKmVisible = false;
      });
    },

    editSubmit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          let req;
          if (this.editForm.subjectId) {
            req = basicsubjectedit;
          } else {
            req = basicsubject;
          }
          req({
            ...this.editForm,
            subSubjectId: this.editForm.subSubjectId.join(","),
          })
            .then((res) => {
              this.$modal.msgSuccess("操作成功");
              this.handleClose();
              this.getList();
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    del(id) {
      this.$modal.confirm("请确认进行删除操作").then(() => {
        basicsubjecteditdelete(id ? id : this.ids).then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.$refs.tableList.clearSelection();
          this.ids = [];
          this.getList();
        });
      });
    },

    debouncedSearch: debounce(function () {
    this.queryParams.pageNum = 1;
      this.getList();
    }, 500),

    handleSearch() {
      this.debouncedSearch();
    },
  },
};
</script>

<style lang="scss" scoped>
.editKmVisible {
  .el-input,
  .el-select {
    width: 240px;
  }
}
</style>